{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SambaNova\n",
    "\n",
    "**[SambaNova](https://sambanova.ai/)'s** [Sambastudio](https://sambanova.ai/technology/full-stack-ai-platform) is a platform for running your own open-source models\n",
    "\n",
    "This example goes over how to use LangChain to interact with SambaNova embedding models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SambaStudio\n",
    "\n",
    "**SambaStudio** allows you to train, run batch inference jobs, and deploy online inference endpoints to run open source models that you fine tuned yourself."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A SambaStudio environment is required to deploy a model. Get more information at [sambanova.ai/products/enterprise-ai-platform-sambanova-suite](https://sambanova.ai/products/enterprise-ai-platform-sambanova-suite)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Register your environment variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "sambastudio_base_url = \"<Your SambaStudio environment URL>\"\n",
    "sambastudio_base_uri = \"<Your SambaStudio environment URI>\"\n",
    "sambastudio_project_id = \"<Your SambaStudio project id>\"\n",
    "sambastudio_endpoint_id = \"<Your SambaStudio endpoint id>\"\n",
    "sambastudio_api_key = \"<Your SambaStudio endpoint API key>\"\n",
    "\n",
    "# Set the environment variables\n",
    "os.environ[\"SAMBASTUDIO_EMBEDDINGS_BASE_URL\"] = sambastudio_base_url\n",
    "os.environ[\"SAMBASTUDIO_EMBEDDINGS_BASE_URI\"] = sambastudio_base_uri\n",
    "os.environ[\"SAMBASTUDIO_EMBEDDINGS_PROJECT_ID\"] = sambastudio_project_id\n",
    "os.environ[\"SAMBASTUDIO_EMBEDDINGS_ENDPOINT_ID\"] = sambastudio_endpoint_id\n",
    "os.environ[\"SAMBASTUDIO_EMBEDDINGS_API_KEY\"] = sambastudio_api_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Call SambaStudio hosted embeddings directly from LangChain!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.embeddings.sambanova import SambaStudioEmbeddings\n",
    "\n",
    "embeddings = SambaStudioEmbeddings()\n",
    "\n",
    "text = \"Hello, this is a test\"\n",
    "result = embeddings.embed_query(text)\n",
    "print(result)\n",
    "\n",
    "texts = [\"Hello, this is a test\", \"Hello, this is another test\"]\n",
    "results = embeddings.embed_documents(texts)\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can manually pass the endpoint parameters and manually set the batch size you have in your SambaStudio embeddings endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings = SambaStudioEmbeddings(\n",
    "    sambastudio_embeddings_base_url=sambastudio_base_url,\n",
    "    sambastudio_embeddings_base_uri=sambastudio_base_uri,\n",
    "    sambastudio_embeddings_project_id=sambastudio_project_id,\n",
    "    sambastudio_embeddings_endpoint_id=sambastudio_endpoint_id,\n",
    "    sambastudio_embeddings_api_key=sambastudio_api_key,\n",
    "    batch_size=32,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or You can use an embedding model expert included in your deployed CoE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings = SambaStudioEmbeddings(\n",
    "    batch_size=1,\n",
    "    model_kwargs={\n",
    "        \"select_expert\": \"e5-mistral-7b-instruct\",\n",
    "    },\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
